<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item size="mini" label="" prop="msgRecordTitle">
        <el-input
          v-model="queryParams.msgRecordTitle"
          placeholder="消息标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini" label="" prop="status">
        <el-select v-model="queryParams.msgRecordState" placeholder="请选择消息状态" clearable>
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item size="mini" label="" prop="msgRecordRecipientName" v-if="isAdmin">
        <el-input
          v-model="queryParams.msgRecordRecipientName"
          placeholder="接收人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-plus" size="mini" @click="showManualPush = true" v-hasPermi="['system:message:push']">手动推送</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-view"
          size="mini"
          @click="sendEditMsgRecord(1)"
        >全部已读
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-reading"
          size="mini"
          @click="sendEditMsgRecord(2)"
        >全部已确认
        </el-button>
      </el-col>
    </el-row>-->


    <el-table v-loading="loading" size="small" :data="msgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="消息标题" min-width="100px" align="left" prop="msgRecordTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="消息内容" min-width="200px" align="left" prop="msgRecordContent" :show-overflow-tooltip="true"/>
      <el-table-column label="接收人" width="100px" align="left" v-if="isAdmin" prop="msgRecordRecipientName" :show-overflow-tooltip="true"/>
      <el-table-column label="消息状态" align="center" prop="msgRecordState" width="90px">
        <el-tag slot-scope="{ row }" size="small" :type="getTagVal(row.msgRecordState)">{{ msgRecordStateFormat(row) }}</el-tag>
      </el-table-column>
      <el-table-column label="消息创建时间" align="center" width="180px" prop="createTime"/>
      <el-table-column label="操作" align="center" width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="750px" append-to-body>
      <el-descriptions class="margin-top" :aria-current="2" :column="1" :size="size" border label-style="width:100px;text-align:center">
        <el-descriptions-item>
          <template slot="label">
            消息标题
          </template>
          {{ form.msgRecordTitle }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            消息内容
          </template>
          {{ form.msgRecordContent }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            已读时间
          </template>
          {{ form.msgRecordReadTime }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            接收时间
          </template>
          {{ form.msgRecordRecipientTime }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            发送时间
          </template>
          {{ form.createTime }}
        </el-descriptions-item>

        <el-descriptions-item>
          <template slot="label">
            消息状态
          </template>
          {{ msgRecordStateFormat(form) }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="form.msgRecordState == 2" v-if="this.user.userId == form.msgRecordRecipient"  @click="handleUpdate(form,2)">确认</el-button>
        <el-button @click="open = false;getList();">取 消</el-button>
      </div>
    </el-dialog>

    <ManualPush v-model="showManualPush" @ok="getList"/>
  </div>
</template>

<script>
import {getMsgRecord, listMsgRecord, editBatchMsgRecord, editMsgRecord} from "@/api/system/message";
import ManualPush from './components/ManualPush'
import auth from '@/plugins/auth'
import {mapGetters}  from "vuex";

export default {
  name: "message",
  components: {ManualPush},
  data() {
    return {
      // 是否管理员
      isAdmin : false,
      // 遮罩层
      loading: true,
      //消息列表
      msgList: [],
      //消息数据量
      total: 0,
      // 选中数组
      msgRecordIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹窗窗口
      open: false,
      //弹窗标题
      title: '',
      // 消息数据
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        msgRecordTitle: undefined,
        msgRecordState: undefined,
        msgRecordRecipientName: undefined
      },
      statusOptions: [],
      showManualPush: false
    }
  },
  computed:{
    ...mapGetters(['user'])
  },
  created() {
    this.isAdmin = auth.hasRole('admin');
    this.getDicts('sys_msg_record_state').then(response => {
      this.statusOptions = response.data
    })
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true
      listMsgRecord(this.queryParams).then(response => {
        this.msgList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    handleUpdate(row, msgRecordState) {
      if (row != undefined) {
        this.open = false;
        row.msgRecordState = msgRecordState;
        editMsgRecord(row).then(response => {
          this.msgSuccess('消息已确认');
          this.getList();
        })
        return;
      }
      if (this.msgRecordSelectList == undefined || this.msgRecordSelectList.length == 0) {
        this.msgInfo('请勾选消息之后,再进行操作!');
        return;
      }
      let result = this.msgRecordSelectList.filter(item => {
        return item.msgRecordState != msgRecordState && item.msgRecordState != 2
      })
      if (result.length == 0) {
        this.msgInfo('不允许重复修改状态!');
        return;
      }
      result.forEach(item => {
        item.msgRecordState = msgRecordState;
      })
      editBatchMsgRecord(result).then(response => {
        if (response.code === 200) {
          if (msgRecordState == 1) {
            this.msgSuccess('勾选的数据已全部修改为已读!');
          } else if (msgRecordState == 2) {
            this.msgSuccess('勾选的数据已全部修改为已确认!');
          }
          this.getList();
        }
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        msgRecordState: undefined
      };
      this.getList();
    },
    handleSelectionChange(selection) {
      this.msgRecordSelectList = selection.map(item => item)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    reset() {
      this.form = {
        msgRecordTitle: undefined,
        msgRecordContent: undefined,
        msgRecordState: undefined,
        msgRecordRecipient: undefined,
        msgRecordRecipientTime: undefined,
        msgRecordReadTime: undefined,
        creteTime: undefined,
      }
      this.resetForm('form')
    },
    handleView(row) {
      this.reset();
      const isAdmin = auth.hasRole('admin')
      getMsgRecord(row.msgRecordId).then(response => {
        let tempForm = response.data;
        this.form = response.data;

        if (response.data.msgRecordState == '0' && (!!!isAdmin || this.form.msgRecordRecipient == this.user.userId) ) {
          tempForm.msgRecordState = '1';
          editMsgRecord(tempForm).then(response => {
            this.form.msgRecordReadTime = new Date().Format("yyyy-MM-dd hh:mm:ss");
          })
        }
        this.open = true;
        this.title = '消息';
      });
    },
    sendEditMsgRecord(msgRecordState) {
      let msgRecordStateText = msgRecordState == 1 ? '已读' : '已确认';
      this.$confirm('此操作将会消息状态全部修改成' + msgRecordStateText + ', 是否继续?', '修改信息状态', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editMsgRecord({"isAll": "1", "msgRecordState": msgRecordState}).then(response => {
          if (response.code = 200) {
            if (msgRecordState == 1) {
              this.msgSuccess('数据已全部修改为已读!');
            } else if (msgRecordState == 2) {
              this.msgSuccess('数据已全部修改为已确认!');
            }
            this.getList();
          }
        });
      })

    },
    msgRecordStateFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.msgRecordState)
    },
    getTagVal(msgRecordState) {
      if (msgRecordState == '0') {
        return 'info';
      }
      if (msgRecordState == '1') {
        return 'warning';
      }
      if (msgRecordState == '2') {
        return 'success';
      }
    }
  },
}
</script>

<style scoped>

</style>
